package com.zrt.pad.network;

import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import android.util.Log;

import com.zrt.pad.datasource.DataSourceInstance;
import com.zrt.pad.datasource.OptionName;
import com.zrt.pad.datasource.bean.SysOption;
import com.zrt.pad.datasource.bean.UsrUser;
import com.zrt.pad.network.response.RespResult;
import com.zrt.pad.util.DESUtil;
import com.zrt.pad.util.SellUtil;

public class NetworkManager {

	public static final NetworkManager nm = new NetworkManager();

	private NetworkManager() {

	}

	public static String tryServerIp(String ip) {
		if (ip.contains("http://"))
			return String.format("%s/", ip);
		else
			return String.format("http://%s/", ip);
	}

	public static void updateServerIp(String ip) {
		if (ip.contains("http://"))
			ServerPrefix = String.format("%s/", ip);
		else
			ServerPrefix = String.format("http://%s/", ip);
	//	long row = DataSourceInstance.instance.setOption(OptionName.SERVER_IP, ip);
	}

	public static String ServerPrefix;

	static {
		String ip = DataSourceInstance.instance.getOption(OptionName.SERVER_IP);
		if (ip == null || "".equals(ip.trim())) {
			ip = "http://192.168.1.211:8080/Smartfmk/comserver";
		}
		updateServerIp(ip);
	}
	
	
	public SysOption[] getSysOption() throws Exception {
		String errorMsg = null;
		try {
			JSONObject req = new JSONObject();
			req.put("funcode", "0001"); //接口编码
			req.put("StationID", "******"); //SellUtil.getStationID()
			req.put("OrgCode", "*");//SellUtil.getOrgCode()
			req.put("UserCode", SellUtil.getUserCode());
			req.put("Passwd", DESUtil.instance.getEncString(SellUtil
					.getUserPwd().toString()));
			String swhere = "";
			req.put("where", swhere);

			String json = Network.instance.post(ServerPrefix
					+ "ComGetMenuInfo", req);
			RespResult rr = RespResult.initRespResult(json);
			if (rr.isSucceed()) {
				JSONArray array = rr.returnArray;
				SysOption[] options = new SysOption[array.length()];
				for (int i = 0; i < options.length; i++) {
					JSONObject obj = array.getJSONObject(i);
					SysOption sysoption = new SysOption();
					sysoption = sysoption.initWithJSON(obj);
					options[i] = sysoption;
				}
				return options;
			} else {
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			if (errorMsg == null) {
				errorMsg = "获取选项信息失败！";
			}
			throw new Exception(errorMsg);
		}
	}

	public UsrUser[] getUsrUser() throws Exception {
		String errorMsg = null;
		try {
			JSONObject req = new JSONObject();
			req.put("funcode", "0002"); //
			req.put("UserCode", SellUtil.getUserCode());
			req.put("Passwd", DESUtil.instance.getEncString(SellUtil
					.getUserPwd().toString()));
			String swhere = "";
			req.put("where", swhere);
			
			String json = Network.instance.post(ServerPrefix
					+ "ComGetMenuInfo", req);
			RespResult rr = RespResult.initRespResult(json);
			if (rr.isSucceed()) {
				JSONArray array = rr.returnArray;
				UsrUser[] users = new UsrUser[array.length()];
				for (int i = 0; i < users.length; i++) {
					JSONObject obj = array.getJSONObject(i);
					UsrUser usr = new UsrUser();
					usr = usr.initWithJSON(obj);
					users[i] = usr;
				}
				return users;
			} else {
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			if (errorMsg == null) {
				errorMsg = "获取用户失败！";
			}
			throw new Exception(errorMsg);
		}
	}
	
	public String checkAppUpdate(String versionName) throws Exception {
		String json = Network.instance.get(ServerPrefix + "",
				"GetPosUpdate");
		JSONObject job = new JSONObject(json);
		if ("1".equals(job.get("succeedFlag"))) {
			String rstr = job.getString("returnInfo");
			JSONObject returnInfo = new JSONObject(rstr);
			if (versionName.equals(returnInfo.getString("version")) == false) {
				return returnInfo.getString("url");
			}
		} else {
			Log.e("CheckAppUpdate", job.getString("errorInfo"));
		}
		return null;
	}
	
	public boolean UserCheckIn() throws Exception {
		String errorMsg = null;
		try {
			JSONObject req = new JSONObject();
			req.put("funcode", "9001"); //接口编码
			req.put("UserCode", SellUtil.getUserCode());
			req.put("Passwd", DESUtil.instance.getEncString(SellUtil
					.getUserPwd().toString()));
			String swhere = "";
			req.put("where", swhere);
			
			String json = Network.instance.post(ServerPrefix
					+ "ComSetMenuInfo", req);
			RespResult rr = RespResult.initRespResult(json);
			if (rr.isSucceed()) {
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			if (errorMsg == null) {
				errorMsg = "服务员登录失败！";
			}
			throw new Exception(errorMsg);
		}
	}
	
	
}
